<?php
/**
 * Created by PhpStorm.
 * User: sakura
 * Date: 2019/4/26
 * Time: 4:56 PM
 */

namespace app\admin\controller;


use app\common\model\Car;
use app\common\model\Orders;
use app\common\model\Users;
use \app\common\model\Driver;

class Order extends Base
{
    public function index()
    {
        $model = new Orders();

        $pageParam = ['query' => []];
        if (isset($this->param['keywords']) && !empty($this->param['keywords'])) {
            $pageParam['query']['keywords'] = $this->param['keywords'];
            $model->whereLike('', "%" . $this->param['keywords'] . "%");
            $this->assign('keywords', $this->param['keywords']);
        }

        if (isset($this->param['_order_']) && !empty($this->param['_order_'])) {
            $pageParam['query']['_order_'] = $this->param['_order_'];
            $order                         = $this->param['_order_'];
            switch ($order) {
                case 'id':
                    $order = 'id';
                    break;
                case 'name':
                    $order = 'name';
                    break;
                case 'reg_time':
                    $order = 'reg_time';
                    break;
                case 'last_login_time':
                    $order = 'last_login_time';
                    break;
                default:
                    $order = 'id';
            }
            $by = isset($this->param['_by_']) && !empty($this->param['_by_']) ? $this->param['_by_'] : 'desc';
            $model->order($order, $by);
            $this->assign('_order_', $this->param['_order_']);
            $this->assign('_by_', $this->param['_by_']);
        } else {
            $model->order('id', 'desc');
        }

        if (isset($this->param['export_data']) && $this->param['export_data'] == 1){
            $header = ['ID', '车主', '车牌号', '乘客', '起始位置', '目的地', '订单金额', '创建时间', '更新时间', '状态'];
            $body   = [];
            $data   = $model->select();
            foreach ($data as $item) {
                $record                    = [];
                $record['id']              = $item->id;
                $record['driver']            = $item->name;
                $record['nickname']        = $item->nickname;
                $record['mobile']          = $item->mobile;
                $record['email']           = $item->email;
                $record['id_card']         = $item->id_card;
                $record['create_time']     = $item->create_time;
                $record['update_time']     = $item->update_time;
                $record['status']          = $item->admin_status_text;
                $body[]                    = $record;
            }
            return $this->export($header, $body, "User-" . date('Y-m-d-H-i-s'), '2007');
        }

        $list = $model->with(['users','driver','car'])->paginate($this->webData['list_rows'], false, $pageParam);
//        return json($list);
        $this->assign([
            'list'      => $list,
            'total'     => $list->total(),
            'page'      => $list->render(),
        ]);
        return $this->fetch();
    }

    public function del()
    {

        $id     = $this->id;
        $result = Orders::destroy(function ($query) use ($id) {
            $query->whereIn('id', $id);
        });
        if ($result) {
            return $this->deleteSuccess();
        }
        return $this->error('删除失败');
    }


    //结算
    public function disable()
    {
        $user         = Orders::get($this->id);
        $user->status = $user->status == 1 ? 0 : 1;
        $result       = $user->save();
        if ($result) {
            return $this->success();
        }
        return $this->error();
    }
}